﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>试卷编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../static/css/font.css">
    <link rel="stylesheet" href="../../../static/css/weadmin.css">
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
    <div class="weadmin-body">
        <form class="layui-form">
            <input type="hidden" id="EPID" name="EPID" />
            <div class="layui-form-item">
                <label for="EPName" class="layui-form-label">
                    <span class="we-red">*</span>试卷名称
                </label>
                <div class="layui-input-block">
                    <input type="text" id="EPName" name="EPName" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="EPTID" class="layui-form-label">
                    <span class="we-red">*</span>试卷类型
                </label>
                <div class="layui-input-inline">
                    <select id="EPTID" name="EPTID" class="valid"></select>
                </div>
                <label for="EPFullMarks" class="layui-form-label">
                    <span class="we-red">*</span>试卷满分
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="EPFullMarks" name="EPFullMarks" lay-verify="required|number" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="DepIDs" class="layui-form-label">
                    <span class="we-red">*</span>考试部门
                </label>
                <div id="DepIDs" class="layui-input-block">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="EPExamDuration" class="layui-form-label">
                    <span class="we-red">*</span>时长(分)
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="EPExamDuration" name="EPExamDuration" lay-verify="required|number" autocomplete="off" class="layui-input">
                </div>
                <label for="EPExaminer" class="layui-form-label">
                    <span class="we-red">*</span>监考人
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="EPExaminer" name="EPExaminer" placeholder="多个人|隔开" autocomplete="off" lay-verify="required" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="EPDescribe" class="layui-form-label">
                    试卷描述
                </label>
                <div class="layui-input-block">
                    <textarea id="EPDescribe" name="EPDescribe" placeholder="请输入" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="EPRoom" class="layui-form-label">
                    <span class="we-red">*</span>考场
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="EPRoom" name="EPRoom" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
                <label for="EPIsRelease" class="layui-form-label">
                    在发布
                </label>
                <div class="layui-input-inline">
                    <input type="checkbox" id="EPIsRelease" name="EPIsRelease" lay-skin="switch" lay-filter="switchIsRelease" lay-text="是|否">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="EPStartTime" class="layui-form-label">
                    <span class="we-red">*</span>开始时间
                </label>
                <div class="layui-input-inline">
                    <input id="EPStartTime" name="EPStartTime" verify="required" autocomplete="off" class="layui-input">
                </div>
                <label for="EPEndTime" class="layui-form-label">
                    <span class="we-red">*</span>结束时间
                </label>
                <div class="layui-input-inline">
                    <input id="EPEndTime" name="EPEndTime" verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="add">确定</button>
                </div>
            </div>
        </form>
    </div>
    <script src="../../../lib/layui/layui.js" charset="utf-8"></script>
    <script src="../../../static/js/common.js" charset="utf-8"></script>

    <script>
        layui.extend({
            selectM: '../../../lib/layui_extends/selectM'
        });
        layui.use(['form', 'jquery', 'layer', 'laydate', 'selectM'], function () {
            var form = layui.form,
                $ = layui.jquery,
                laydate = layui.laydate,
                selectM = layui.selectM,
                layer = layui.layer;
            $.support.cors = true;
            $.ajaxSetup({
                cache: false,
                headers: {
                    "userid": JSON.parse(window.localStorage.authInfo).UserId,
                    "signtoken": JSON.parse(window.localStorage.authInfo).SignToken,
                },
            })
            laydate.render({
                elem: '#EPStartTime',
            });
            laydate.render({
                elem: '#EPEndTime',
            });
            $(function () {
                //加载部门下拉框
                var depdata = [];
                var deplist = $.ajax({
                    url: ApiUrl + "Dep/Sel",
                    type: 'post',
                    success: function (data) {
                        if (data.StatusCode == 200) {
                            depdata = data.Data;
                        }
                    }
                });
                //加载试卷类型下拉框
                var localinfoajax = $.ajax({
                    url: ApiUrl + "ExamPaperType/GetList",
                    type: 'get',
                    success: function (data) {
                        if (data.StatusCode == 200) {
                            $(data.Data).each(function (index, item) {
                                $("#EPTID").append("<option value='" + item.EPTID + "'>" + item.EPTName + "</option>");
                            });
                            form.render();
                        }
                    }
                });

                //加载试卷信息
                var editdata = {};
                var infoajax = $.ajax({
                    url: ApiUrl + "ExamPaperMain",
                    type: 'get',
                    data: { id: getUrlParam("ID") },
                    success: function (data) {
                        if (data.StatusCode == 200) {
                            editdata = data.Data;
                        }
                    }
                });
                //相关ajax加载完成后，绑定数据
                $.when(deplist,localinfoajax, infoajax)
                    .done(function () {
                        for (var key in editdata) {
                            if (key == "EPIsRelease") {
                                $("#" + key).prop("checked", editdata[key] == "1" ? true : false);
                            }
                            else if (key == "EPStartTime" || key == "EPEndTime") {
                                $("#" + key).val($.trim(editdata[key]).substring(0, 10));
                            }
                            else if (key == "DepIDs") {
                                var seldepids = editdata[key];
                                seldepids = seldepids.length > 0 ? seldepids.substr(1, seldepids.length - 2) : "";
                                selectM({
                                    //元素容器【必填】
                                    elem: '#DepIDs',
                                    tips: '请选择考试部门',
                                    //候选数据【必填】
                                    data: depdata,
                                    selected: seldepids.split(","),
                                    field: {
                                        idName: 'DepID',
                                        titleName: 'DepName'
                                    },
                                    max: 100,
                                    //添加验证
                                    verify: 'required'
                                });
                            }
                            else {
                                $("#" + key).val($.trim(editdata[key]));
                            }
                        }
                        form.render();
                    })
                    .fail(function () {
                    });
            })
            //监听提交
            form.on('submit(add)', function (formdata) {
                //console.log(formdata.field)
                //处理开关返回值，也可通过设置value='1'让选中时返回的on改为1，但未选中时，仍不返回值，需要model接收时使用int?得到默认值0，因此在此处理比较安全
                formdata.field.EPIsRelease = formdata.field.EPIsRelease == "on" ? "1" : "0";
                formdata.field.DepIDs = "," + formdata.field.DepIDs + ",";
                $.ajax({
                    url: ApiUrl + "ExamPaperMain/Edit",
                    type: 'post',
                    dataType: 'json',
                    data: formdata.field,
                    success: function (data) {
                        if (data.StatusCode == 200 && data.Count != 0) {
                            layer.alert(data.Info, { icon: 6 }, function (index) {
                                parent.layer.closeAll();
                            })
                        }
                        else if (data.StatusCode == 403) {
                            top.layer.alert(data.Info + ",请重新登录！", { closeBtn: 0 }, function () {
                                top.location.href = "http://" + top.location.host + "/login.html";
                            })
                        }
                        else {
                            layer.msg(data.Info);
                        }
                    }
                });
                return false;
            });
        });
    </script>
</body>
</html>